#include <iostream>
#include "Acceptor.h"
#include "TcpConnection.h"
#include "EventLoop.h"


using std::cout;
using std::endl;

void testV1()
{
    Acceptor acceptor("127.0.0.1", 8888);
    acceptor.ready();

    TcpConnection tcp(acceptor.accept());
    tcp.toString();
    cout << "has connected." << endl;
    while(1)
    {
        cout << ">>recv from client :" << tcp.receive() << endl;
        tcp.send("i have received.\n");
    }
}

using TcpConnectionPtr = shared_ptr<TcpConnection>;
using TcpConnectionCallback = function<void(const TcpConnectionPtr &)>;

void onNewConnection(const TcpConnectionPtr &con)
{
    con->toString();
    cout << " has connected." << endl;
}

void onMessage(const TcpConnectionPtr &con)
{
    cout << ">>recv from client :" << con->receive() << endl;
    con->send("i have received.\n");
}

void onClose(const TcpConnectionPtr &con)
{
    con->toString();
    cout << " has closed.." << endl;
}

void testV2()
{
    Acceptor acc("127.0.0.1", 8888);
    acc.ready();
    EventLoop loop(acc);
    loop.setNewConnectionCallback(std::move(onNewConnection));
    loop.setMessageCallback(std::move(onMessage));
    loop.setCloseCallback(std::move(onClose));
    loop.loop();
}
int main(int argc,char *argv[])
{
    testV2();
    return 0;
}


